home *** CD-ROM | disk | FTP | other *** search
/ The 640 MEG Shareware Studio 4 / The 640 Meg Shareware Studio CD-ROM Volume IV (Data Express)(1994).ISO / clang / cuj0593.zip / 1105051A < prev    next >
Text File  |  1993-05-16  |  2KB  |  89 lines

  1. #include <iostream.h>
  2. #include <stdlib.h>
  3.  
  4. #include "os.h"
  5.  
  6. #undef VERBOSE
  7.  
  8. extern unsigned int _stklen = 24576;
  9.  
  10. int
  11. compare_ints(const void *a, const void *b)
  12. {
  13.     return *(int *) a - *(int *) b;
  14. }
  15.  
  16. const int NumInts = 1000;
  17. const int NumReps = 1000;
  18.  
  19. int
  20. main(int argc, char *argv[])
  21. {
  22.     int i;
  23.     int *arr = new int[NumInts];
  24.     int seed;
  25.     OSTree<int> *tree;
  26.  
  27.     for (int j = 0; j < NumReps; j++) {
  28.     tree = new OSTree<int>;
  29.     cout << "Repetition " << j << '\n';
  30.     switch (argc) {
  31.         case 1:
  32.         if (NumReps == 1)
  33.             randomize();
  34.         else
  35.             srand(seed = j);
  36.         break;
  37.        case 2:
  38.         srand(atoi(argv[1]));
  39.         break;
  40.        case 3:
  41.         cerr << "Usage: " << argv[0] << " [seed]\n";
  42.         exit(1);
  43.     }
  44.  
  45.     for (i = 0; i < NumInts; i++) {
  46.         arr[i] = rand();
  47.         tree->Insert(arr[i]);
  48.         tree->CheckNodes();
  49. #ifdef VERBOSE
  50.         tree->PrintNodes();
  51.         cout << '\n';
  52. #endif
  53.     }
  54. #ifdef VERBOSE
  55.     cout << '\n';
  56. #endif
  57.  
  58.     qsort(arr, NumInts, sizeof(int), compare_ints);
  59.  
  60.     for (i = 0; i < NumInts; i++) {
  61.         int inx;
  62.         int j, k;
  63.         for (j = 0; j < NumInts - i; j++)
  64.         if (*tree->Select(j) != arr[j]) {
  65.             cout << "Problems, seed = " << seed << '\n';
  66.         }
  67.         inx = rand() % (NumInts - i);
  68.         tree->DeleteItem(arr[inx]);
  69.         tree->CheckNodes();
  70. #ifdef VERBOSE
  71.         tree->PrintNodes();
  72. #endif
  73.         for (j = inx; j < NumInts - 1; j++)
  74.             arr[j] = arr[j+1];
  75. #ifdef VERBOSE
  76.         cout << *tree->Select(i) << ' ';
  77.         cout << '\n';
  78. #endif
  79.         }
  80. #ifdef VERBOSE
  81.     cout << '\n';
  82. #endif
  83.     delete tree;
  84.     }
  85.  
  86.     return 0;
  87. }
  88.  
  89.